package com.xxx.para;

import java.lang.reflect.Field;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ParametersUtil {
	/**
	 * 验证字符中否有空格
	 * 
	 * @param str
	 * @return
	 */
	public static boolean checkTrim(String str) {
		String rea = "^[^\\s]*";
		Pattern p = Pattern.compile(rea);
		Matcher m = p.matcher(str);
		return m.matches();
	}

	public static boolean isNotNull(Object o){
		return !isNull(o);
	}

	/**
	 * 判断对象是否为null 集合和数组如果大小为0，也返回true 字符串如果是空字符串或者"null",返回true
	 * 
	 * @param o
	 * @return
	 */
	public static boolean isNull(Object o) {
		if (o == null) {
			return true;
		}

		if (o instanceof String) {
			String s = (String) o;
			if (s.trim().equals("")) {
				return true;
			}
			if (s.trim().equals("null")) {
				return true;
			}
			if (s.trim().equals("-1")) {
				return true;
			}
			return false;
		} else if (o instanceof Object[]) {
			return ((Object[]) o).length == 0;
		} else if (o instanceof Collection<?>) {
			return ((Collection<?>) o).isEmpty();
		} else if (o instanceof Map<?, ?>) {
			return ((Map<?, ?>) o).isEmpty();
		}
		return false;
	}
	
	
	/**
	 * 判断两个对象是否相等
	 * 
	 * @param o1
	 * @param o2
	 * @return
	 */
	public static boolean equal(Object o1, Object o2) {
		if (o1 == o2) {
			return true;
		}
		if (o1 != null && o1.equals(o2)) {
			return true;
		}
		return false;
	}
	
	
	static Pattern pattern = Pattern.compile("\\d+\\.?\\d*|\\d*\\.?\\d+");
	
	/**
	 * 判断一个字符串是否为数字
	 * 
	 * @param s
	 * @return
	 */
	public static boolean isNumber(String s) {
		if (isNull(s)) {
			return false;
		}
		Matcher matcher = pattern.matcher(s);
		if (!matcher.matches()) {
			return false;
		}

		return true;
	}

	/**
	 * 验证一个字符串能否被转换成相应的整数、浮点数
	 * 
	 * @param s
	 * @param type
	 * @return
	 */
	public static boolean isInvalidateNumber(String s, Class<?> type) {
		if (isNull(s)) {
			return false;
		}

		try {
			if (type == byte.class) {
				Byte.parseByte(s);
			} else if (type == short.class) {
				Short.parseShort(s);
			} else if (type == int.class) {
				Integer.parseInt(s);
			} else if (type == long.class) {
				Long.parseLong(s);
			} else if (type == float.class) {
				Float.parseFloat(s);
			} else if (type == double.class) {
				Double.parseDouble(s);
			}
		} catch (NumberFormatException e) {
			return false;
		}
		return true;
	}

	/**
	 * 字符串分隔为set
	 * 
	 * @param s
	 * @param split
	 * @return
	 */
	public static Set<String> string2Set(String s, String split) {
		Set<String> set = new HashSet<String>();
		String arr[] = s.split(split);
		for (int i = 0; i < arr.length; i++) {
			set.add(arr[i]);
		}
		return set;
	}

	/**
	 * 对象参数转Map
	 * @param objClass
	 * @return
	 */
	public static Map<String,Object> objcetToMap(Object objClass){
		Map<String,Object> map = new HashMap<String, Object>();
		Field[] fields = objClass.getClass().getDeclaredFields();
		for (Field field : fields) {
			Object obj;
			try {
				field.setAccessible(true);
				obj = field.get(objClass);
				if(obj!=null){
					map.put(field.getName(), obj);
				}
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}
		}
		return map;
	}

}
